package edu.olin.scheduler.common;
//Annie Martin
//March 2007
//Schedualer
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;


public class Schedule {
    private ArrayList<Course> courses;
    
    //interface for objects that should be notified when a Schedule is changed
    public interface Listener { public void scheduleChanged(); }

    public Schedule() {
        courses=new ArrayList();
        }

    //Change Listeners
    private ArrayList<Listener> listeners;
    
    //Register a new Listener
    public void addListener(Listener el){
        //whenever a GUIPreference is changed, the registered listeners will have their repaint() methods invoked
        if(listeners==null){ listeners = new ArrayList(); }
        listeners.add(el);
        }
    
    //Removes the given Listener from the list of registered listeners
    public void removeListener(Listener el){
        if(listeners==null){ listeners = new ArrayList(); }
        while(listeners.remove(el)){} //remove every instance encountered
        }
    
    //Notifies all of the listeners that a preference has changed
    private void notifyListeners(){
        if(listeners==null){ listeners = new ArrayList(); return; }
        Iterator<Listener> itr = listeners.iterator();
        while(itr.hasNext()){
            Listener el = itr.next();
            if(el!=null){ el.scheduleChanged(); }
            }
        }
    
    public void addCourse(Course c) {
        courses.add(c);
        notifyListeners();
        }
    
    public boolean removeCourse(Course c) {
        boolean flag = false;
        while(courses.remove(c)){ flag=true; } //remove all instances
        notifyListeners();
        return flag;
        }
    
    public ArrayList<Course> getCourses(){
        return courses;
        }
    
    public void clear(){
        courses.clear();
        notifyListeners();
        }
    
}